#include <iostream>
#include <vector>
#include <chrono>
#include <curl/curl.h>

// 回调函数，用于处理接收到的数据
static size_t writeCallback(void *contents, size_t size, size_t nmemb, std::string *s)
{
    size_t totalSize = size * nmemb;
    s->append((char *)contents, totalSize);
    return totalSize;
}

int main()
{
    // 初始化 libcurl
    curl_global_init(CURL_GLOBAL_DEFAULT);

    // 创建 multi 句柄
    CURLM *multi_handle = curl_multi_init();
    if (!multi_handle)
    {
        std::cerr << "Failed to create CURLM handle" << std::endl;
        return 1;
    }

    // 定义 URL 列表
    const char *url = "https://example.com";
    std::vector<CURL *> easy_handles;
    std::vector<std::string> responses(100);

    for (int count = 0; count < 5; count++)
    {
        // 创建 100 个 CURL 句柄并添加到 multi 句柄中
        for (int i = 0; i < 100; i++)
        {
            CURL *easy_handle = curl_easy_init();
            if (easy_handle)
            {
                curl_easy_setopt(easy_handle, CURLOPT_URL, url);
                curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writeCallback);
                curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &responses[i]);
                curl_multi_add_handle(multi_handle, easy_handle);
                easy_handles.push_back(easy_handle);
            }
            else
            {
                std::cerr << "Failed to create CURL handle" << std::endl;
            }
        }

        getchar();

        std::cout << count + 1 << ": 任务已提交，等待完成..." << std::endl;

        int still_running = 0;
        do
        {
            // 执行并发请求
            curl_multi_perform(multi_handle, &still_running);

            // 等待活动事件，避免忙等待
            int numfds;
            curl_multi_wait(multi_handle, nullptr, 0, 1000, &numfds);

            // 处理已完成的事件
            CURLMsg *msg;
            int msgs_left;
            while ((msg = curl_multi_info_read(multi_handle, &msgs_left)))
            {
                if (msg->msg == CURLMSG_DONE)
                {
                    CURL *easy_handle = msg->easy_handle;
                    int index = std::distance(easy_handles.begin(), std::find(easy_handles.begin(), easy_handles.end(), easy_handle));

                    if (msg->data.result == CURLE_OK)
                    {
                        std::cout << "任务 " << index << " 已完成: " << std::endl;
                    }
                    else
                    {
                        std::cerr << "任务 " << index << " 失败: " << curl_easy_strerror(msg->data.result) << std::endl;
                    }

                    // 清理资源
                    curl_multi_remove_handle(multi_handle, easy_handle);
                    curl_easy_cleanup(easy_handle);
                    easy_handles[index] = nullptr;
                }
            }
        } while (still_running);

        std::cout << count + 1 << ": 所有任务已完成！" << std::endl;
    }

    // 清理 multi 句柄
    curl_multi_cleanup(multi_handle);

    // 清理全局资源
    curl_global_cleanup();

    getchar();

    return 0;
}